package iDirection;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Polygon;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

public class ImagineHandle {

	BufferedImage bufferedImage, bufferedImageBackup;

	Image image;

	ImagineHandle() {

		bufferedImage = null;

		bufferedImageBackup = null;

		image = null;
	}

	public void loadFile(String dir) {
		String infile = dir + "map" + ".jpg";
		try {
			File input = new File(infile);
			bufferedImage = ImageIO.read(input);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}

	public void drawDirection(int [] test) {

		bufferedImageBackup = bufferedImage;

		Graphics2D gp = bufferedImage.createGraphics();
		gp.setColor(Color.red);
		gp.setStroke(new BasicStroke(5));
		int length = test.length;
		for (int i=0; i+3 < length; i = i+2) {
		gp.drawLine(test[i], test[i+1], test[i+2], test[i+3]); //something to change
		}
		drawArrow(gp, test[length-4], test[length-3], test[length-2], test[length-1]);
	}

	public void saveFile(String dir, int [] test) {

		try {
			String outfile = dir + Integer.toString(test[0])+Integer.toString(test[1])+".jpg";
			File outputFile = new File(outfile);
			ImageIO.write(bufferedImage, outfile.substring(
					outfile.length() - 3, outfile.length()), outputFile);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}

	public void drawArrow(Graphics2D g2, int x1, int y1, int x2, int y2) {

		g2.drawPolygon(getArrow(x1, y1, x2, y2, 9, 0, 0.5)); // startx,starty,endx,endy,headsize,difference,factor

		g2.fillPolygon(getArrow(x1, y1, x2, y2, 9, 0, 0.5));
	}

	public Polygon getArrow(int x1, int y1, int x2, int y2, int headsize,
			int difference, double factor) {

		int[] crosslinebase = getArrowHeadLine(x1, y1, x2, y2, headsize);

		int[] headbase = getArrowHeadLine(x1, y1, x2, y2, headsize - difference);

		int[] crossline = getArrowHeadCrossLine(crosslinebase[0],
				crosslinebase[1], x2, y2, factor);

		Polygon head = new Polygon();

		head.addPoint(headbase[0], headbase[1]);

		head.addPoint(crossline[0], crossline[1]);

		head.addPoint(x2, y2);

		head.addPoint(crossline[2], crossline[3]);

		head.addPoint(headbase[0], headbase[1]);

		head.addPoint(x1, y1);

		return head;
	}

	public int[] getArrowHeadLine(int xsource, int ysource, int xdest,
			int ydest, int distance) {
		int[] arrowhead = new int[2];

		int headsize = distance;

		double stretchfactor = 0;

		stretchfactor = 1 - (headsize / (Math
				.sqrt(((xdest - xsource) * (xdest - xsource))
						+ ((ydest - ysource) * (ydest - ysource)))));

		arrowhead[0] = (int) (stretchfactor * (xdest - xsource)) + xsource;

		arrowhead[1] = (int) (stretchfactor * (ydest - ysource)) + ysource;

		return arrowhead;
	}

	public int[] getArrowHeadCrossLine(int x1, int x2, int b1, int b2,
			double factor) {
		int[] crossline = new int[4];

		int xdest = (int) (((b1 - x1) * factor) + x1);

		int ydest = (int) (((b2 - x2) * factor) + x2);

		crossline[0] = (int) ((x1 + x2 - ydest));

		crossline[1] = (int) ((x2 + xdest - x1));

		crossline[2] = crossline[0] + (x1 - crossline[0]) * 2;

		crossline[3] = crossline[1] + (x2 - crossline[1]) * 2;

		return crossline;

	}

}
